package com.lzj.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzj.admin.dto.TreeDto;
import com.lzj.admin.mapper.*;
import com.lzj.admin.pojo.PurchaseList;
import com.lzj.admin.pojo.Role;
import com.lzj.admin.pojo.RoleMenu;
import com.lzj.admin.pojo.User;
import com.lzj.admin.service.PurchaseListService;
import com.lzj.admin.service.impl.RoleMenuServiceImpl;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.junit4.SpringRunner;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
class ApplicationTests extends ServiceImpl<RoleMenuMapper, RoleMenu> {

    @Autowired
    private PurchaseListMapper purchaseListMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuServiceImpl roleMenuService;

    @Autowired
    private PurchaseListService purchaseListService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Test
    void contextLoads() {
        List<User> listuser = userMapper.listuser();
        System.out.println(listuser);
    }

    @Test
    void contextLoads2() {
        User admin = userMapper.oneUser("admin");
        System.out.println(admin);
    }

    @Test
    void updateTest() {
        User user = new User();
        user.setId(2);
        user.setUserName("wuyanzu");
        int result = userMapper.updateUserInfo(user);
        System.out.println(result);
    }

    @Test
    void updateUserPasswordByIdTest() {
        int result = userMapper.updateUserPasswordById(2, "66666");
        System.out.println(result);
    }

    @Test
    void queryExUserTest() {
        List<User> admin = userMapper.queryExUser("marry");
        System.out.println(admin);
    }

    @Test
    void insertTest() {
        User user = new User();
        user.setIsDel(0);
        user.setRemarks("sdasdasd");
        user.setUserName("sadasdksadasd");
        user.setBz("ddsadasdsad");
        System.out.println(user);
        userMapper.insertUser(user);
    }

    @Test
    void deleteUserTest() {
        int result = userMapper.deleteUser(10);
        System.out.println(result);
    }

    @Test
    void roleTest() {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("name", "管理员");
        List<Role> roles = roleMapper.selectList(roleQueryWrapper);
        System.out.println(roles);
        Role byId = roleMapper.selectById(2);
        System.out.println(byId);
    }

    @Test
    public void queryTest() {
        List<Map<String, Object>> maps = roleMapper.queryAllRoles(1);
        System.out.println(maps);
    }

    @Test
    public void queryAllMenuTest() {
        List<TreeDto> treeDtos = menuMapper.queryAllMenu();
        System.out.println(treeDtos);
    }

    @Test
    public void arrayListTest() {
        List<String> roleNames = new ArrayList<>();
        List<String> authorities = roleMenuService.findAuthoritiesByRoleName(roleNames);
        for (String authority : authorities) {
            System.out.println(authority);
        }
    }

    @Test
    public void encodeTest() {
        String encodedPassword = passwordEncoder.encode("123456");
        boolean matches = passwordEncoder.matches("123456", "$2a$10$hXGeVoEokYspdLD8HNKPL.Ta/E12emjEW/GYJkWd/BZTsA8HWdKzW");
        System.out.println(matches);
    }

    @Test
    public void testPurchaseList() {
        PurchaseList purchaseList = purchaseListMapper.selectById(29);
        System.out.println(purchaseList);

        SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        Date date = null;
        try {
            date = formatter.parse("Wed Jan 21 00:00:00 CST 2024");  // Ensure this date string matches the formatter pattern
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (date != null) {
            purchaseList.setId(99);
            purchaseList.setPurchaseDate(date);

            SimpleDateFormat newFormatter = new SimpleDateFormat("yyyy-MM-dd");
            Date currentDate = new Date(System.currentTimeMillis());
            System.out.println(newFormatter.format(date));
            int comparisonResult = newFormatter.format(purchaseList.getPurchaseDate()).compareTo(newFormatter.format(currentDate));
            System.out.println(comparisonResult);
        } else {
            System.out.println("Date parsing failed.");
        }
    }


    @Test
    public void testGetNextPurchaseNumber() {
        String nextPurchaseNumber = purchaseListService.getNextPurchaseNumber();
        System.out.println(nextPurchaseNumber);
    }
}
